package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import common.ServerResponse;
import service.IShoppingcartService;
import service.impl.ShoppingcartServiceImpl;
import util.myUtil;

@WebServlet("/cart")
public class shoppingcartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		myUtil.setHead(request, response);
		String type=request.getParameter("type");
		System.out.println("type:"+type);
		if("list".equals(type)) {
			list(request,response);
		}
		else if("add".equals(type)) {
			add(request,response);
		}
		else if("update".equals(type)) {
			update(request,response);
		}
		else if("delete_product".equals(type)) {
			delete_product(request,response);
		}
		else if("select".contentEquals(type)) {
			select(request,response);
		}
		else if("un_select".contentEquals(type)) {
			un_select(request,response);
		}
		else if("get_cart_product_count".equals(type)) {
			get_cart_product_count(request,response);
		}
		else if("select_all".contentEquals(type)) {
			System.out.println("这个特殊比较正确");
			select_all(request,response);
		}
		else if("un_select_all".contentEquals(type)) {
			un_select_all(request,response);
		}
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}
	
	  //操作区
		/****************5.1购物车List列表******************/
		protected void list(HttpServletRequest request, HttpServletResponse response) {
			//从session中获取user_id
			HttpSession session = request.getSession();
			Object obj_session_user_id = session.getAttribute("id");
			//执行购物车list逻辑
			IShoppingcartService shoppingService = new ShoppingcartServiceImpl();
			ServerResponse se = shoppingService.list(obj_session_user_id);
			//将ServerResponse转为json并通过流写出
			myUtil.getjson(se,response);
		}
		/****************5.2购物车添加商品*****************/
		protected void add(HttpServletRequest request, HttpServletResponse response) {
			//从session中获取user_id
			HttpSession session = request.getSession();
			Object obj_session_user_id = session.getAttribute("id");
			
			String product_id=request.getParameter("product_id"); 
			String count=request.getParameter("count");
			
			//执行购物车add逻辑
			IShoppingcartService shoppingService = new ShoppingcartServiceImpl();
			ServerResponse se = shoppingService.add(product_id,count,obj_session_user_id);
			//将ServerResponse转为json并通过流写出
			myUtil.getjson(se,response);
		}
		/****************5.3更新购物车某个产品数量*****************/
		protected void update(HttpServletRequest request, HttpServletResponse response) {
			//从session中获取user_id
			HttpSession session = request.getSession();
			Object obj_session_user_id = session.getAttribute("id");
			
			String product_id=request.getParameter("product_id"); 
			String count=request.getParameter("count");
			
			//执行购物车update逻辑
			IShoppingcartService shoppingService = new ShoppingcartServiceImpl();
			ServerResponse se = shoppingService.update(product_id,count,obj_session_user_id);
			//将ServerResponse转为json并通过流写出
			myUtil.getjson(se,response);
		}
	
		//购物车取消选中某个商品
		private void un_select(HttpServletRequest request, HttpServletResponse response) {
					// TODO Auto-generated method stub
			HttpSession session = request.getSession();
			Object obj_session_user_id = session.getAttribute("id");

			String product_id=request.getParameter("product_id"); 
			IShoppingcartService shoppingService = new ShoppingcartServiceImpl();
			ServerResponse se = shoppingService.un_select(product_id, obj_session_user_id);
					//将ServerResponse转为json并通过流写出
			myUtil.getjson(se,response);
		}

		//购物车选中某个商品
		private void select(HttpServletRequest request, HttpServletResponse response) {
					// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		Object obj_session_user_id = session.getAttribute("id");
					
		String product_id=request.getParameter("product_id"); 
		IShoppingcartService shoppingService = new ShoppingcartServiceImpl();
		ServerResponse se = shoppingService.select(product_id, obj_session_user_id);
					//将ServerResponse转为json并通过流写出
		myUtil.getjson(se,response);
					
		}

		//移除购物车某个产品
		private void delete_product(HttpServletRequest request, HttpServletResponse response) {
					// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		Object obj_session_user_id = session.getAttribute("id");
								
		String product_id=request.getParameter("product_id"); 
		IShoppingcartService shoppingService = new ShoppingcartServiceImpl();
		ServerResponse se = shoppingService.delete_product(product_id, obj_session_user_id);
					//将ServerResponse转为json并通过流写出
		myUtil.getjson(se,response);
					
		}
		
	
	//查询在购物车里面的商品数量
	protected void get_cart_product_count(HttpServletRequest request, HttpServletResponse response) {
		HttpSession session=request.getSession();
		ServerResponse se=null;
		if(session.getAttribute("id")==null) {
			se=ServerResponse.serverResponseByError("error:用户未登录");
		}else {
			String userid=session.getAttribute("id").toString();
			IShoppingcartService s=new ShoppingcartServiceImpl();
			se=s.get_cart_product_count(userid);
		}
		
		myUtil.getjson(se, response);
		
	}
	
	//全选
	protected void select_all(HttpServletRequest request, HttpServletResponse response) {
		HttpSession session=request.getSession();
		ServerResponse se=null;
		if(session.getAttribute("id")==null) {
			se=ServerResponse.serverResponseByError("error:用户未登录");
		}else {
			String userid=session.getAttribute("id").toString();
			IShoppingcartService s=new ShoppingcartServiceImpl();
			se=s.select_all(userid);
		}
		myUtil.getjson(se, response);
		
	}
	//取消全选
	protected void un_select_all(HttpServletRequest request, HttpServletResponse response) {
		HttpSession session=request.getSession();
		ServerResponse se=null;
		if(session.getAttribute("id")==null) {
			se=ServerResponse.serverResponseByError("error:用户未登录");
		}else {
			String userid=session.getAttribute("id").toString();
			IShoppingcartService s=new ShoppingcartServiceImpl();
			se=s.un_select_all(userid);
		}
		myUtil.getjson(se, response);
	}

}
